package com.hengtuo.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.hengtuo.adapter.NeardAdapter;
import com.hengtuo.base.BaseFragment;
import com.hengtuo.kaiyuanzhongguo.R;
import com.hengtuo.util.ObtainNetwork;
import com.hengtuo.util.Url;
import com.hengtuo.view.RefreshLayout;
import com.hengtuo.vo.Eveunt;
import com.hengtuo.vo.Exercised;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.thoughtworks.xstream.XStream;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 近期活动  近期活动 http://www.oschina.net/action/api/event_list?pageIndex=1&uid=0&pageSize=10
 */
public class NearFragment extends BaseFragment{

    private ListView listView;
    private RefreshLayout refreshLayout;
    private ProgressBar progressbar = null;
    private List<Eveunt> list = new ArrayList<>();
    private HttpUtils utils;
    private NeardAdapter adapter;


    @Override
    protected int getLoadViewId() {
        return R.layout.fragment_near;
    }

    @Override
    protected void initView(View view) {

        listView = (ListView)view.findViewById(R.id.near_listview);
        refreshLayout = (RefreshLayout)view.findViewById(R.id.swiped_layout);

    }

    @Override
    protected void initData() {

        getData();

        refreshLord();
    }


    private void refreshLord() {

        refreshLayout
                .setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                        android.R.color.holo_orange_light, android.R.color.holo_red_light);


        // 设置下拉刷新监听器
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                refreshLayout.postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        getData();
                        // 更新完后调用该方法结束刷新
                        refreshLayout.setRefreshing(false);
                    }
                }, 1000);
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {



            }
        });

    }



    private void getData() {

        utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, "http://www.oschina.net/action/api/event_list?pageIndex=1&uid=0&pageSize=10", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String str = responseInfo.result;
                XStream xstream = new XStream();
                xstream.processAnnotations(Exercised.class);
                Exercised e = (Exercised) xstream.fromXML(str);
                list = e.getEvents().getEvent();
                adapter = new NeardAdapter(getActivity(),list);
                listView.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}
